//导入
const express = require('express');
const app = express();
var bodyParser = require('body-parser')
const cors = require('cors');
//定义端口号
var port = process.env.PORT || 8000;
//使用中间件
app.use(cors())
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// 解析 application/json
app.use(bodyParser.json())
// 解析 application/x-www-form-urlencoded
app.use((req, res, next) => {
   res.cc = (res,status=1) => {
    res.send({
        status,
        message:err instanceof Error ? err.message : err,
    })
   } 
   next()
})
// 解析 token 的中间件
const jwtConfig = require('./jwt_config/index')
const {expressjwt:jwt} = require('express-jwt')
// 定义一个错误级别常量
app.use(jwt({
    secret:jwtConfig.jwtSecretKey,
    algorithms:['HS256']
}).unless({
    path:[
        /^\/api\/users\//,
        /^\/api\/sys\//
    ]
}))
// 导入并使用用户路由模块
const userRouter = require('./router/users');
app.use('/api/users', userRouter);

app.listen(port, () => {
    console.log('http://127.0.0.1:'+port)
})